package com.csnj.admin.controller.SystemManager;

import com.csnj.common.entity.APIResult;
import com.csnj.common.rest.BaseController;
import com.csnj.common.utils.TreeUtil;
import com.csnj.core.Srvs.SysManager.SysMenuSrvs;
import com.csnj.core.Srvs.SysManager.SysUserSrvs;
import com.csnj.core.constant.CoreConstants;
import com.csnj.core.entity.SysMenu;
import com.csnj.core.entity.VO.AuthorityMenuTree;
import com.csnj.core.entity.VO.MenuTree;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController extends BaseController<SysMenuSrvs, SysMenu> {
    @Autowired
    private SysUserSrvs userBiz;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResult list(String title) {
        Example example = new Example(SysMenu.class);
        if (StringUtils.isNotBlank(title)) {
            example.createCriteria().andLike("title", "%" + title + "%");
        }
        return new APIResult(baseBiz.selectByExample(example));
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public APIResult getTree(String title) {
        Example example = new Example(SysMenu.class);
        if (StringUtils.isNotBlank(title)) {
            example.createCriteria().andLike("title", "%" + title + "%");
        }
        return new APIResult(getMenuTree(baseBiz.selectByExample(example), CoreConstants.ROOT));
    }

    @RequestMapping(value = "/system", method = RequestMethod.GET)
    public APIResult getSystem() {
        SysMenu menu = new SysMenu();
        menu.setParentId(CoreConstants.ROOT);
        return new APIResult(baseBiz.selectList(menu));
    }

    @RequestMapping(value = "/menuTree", method = RequestMethod.GET)
    public APIResult listMenu(Integer parentId) {
        try {
            if (parentId == null) {
                parentId = getRootMenuId();
            }
        } catch (Exception e) {
            return new APIResult(new ArrayList<MenuTree>());
        }
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        Example example = new Example(Menu.class);
        SysMenu parent = baseBiz.selectById(parentId);
        example.createCriteria().andLike("path", parent.getPath() + "%").andNotEqualTo("id",parent.getId());
        return new APIResult(getMenuTree(baseBiz.selectByExample(example), parent.getId()));
    }

    @RequestMapping(value = "/authorityTree", method = RequestMethod.GET)
    public APIResult listAuthorityMenu() {
        List<AuthorityMenuTree> trees = new ArrayList<AuthorityMenuTree>();
        AuthorityMenuTree node = null;
        for (SysMenu menu : baseBiz.selectListAll()) {
            node = new AuthorityMenuTree();
            node.setText(menu.getTitle());
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        }
        return new APIResult(TreeUtil.bulid(trees, CoreConstants.ROOT));
    }

    @RequestMapping(value = "/user/authorityTree", method = RequestMethod.GET)
    public APIResult listUserAuthorityMenu(Integer parentId, Integer userId){
        try {
            if (parentId == null) {
                parentId = getRootMenuId();
            }
        } catch (Exception e) {
            return new APIResult(new ArrayList<MenuTree>());
        }
        return new APIResult(getMenuTree(baseBiz.getUserAuthorityMenuByUserId(userId),parentId));
    }

    @RequestMapping(value = "/user/system", method = RequestMethod.GET)
    public APIResult listUserAuthoritySystem(Integer userId) {
        return new APIResult(baseBiz.getUserAuthoritySystemByUserId(userId));
    }

    private Integer getRootMenuId() {
        SysMenu menu = new SysMenu();
        menu.setParentId(CoreConstants.ROOT);
        return baseBiz.selectList(menu).get(0).getId();
    }

    private List<MenuTree> getMenuTree(List<SysMenu> menus,int root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (SysMenu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            node.setLabel(menu.getTitle());
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }


}
